<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="150px"
      label-position="left"
    >
      <el-form-item label="分类名称" prop="class_id">
        <el-select clearable
          v-model="form.class_id"
          placeholder="请选择分类名称"
          style="width: 100%"
        >
          <el-option
            v-for="(item, index) in classList"
            :key="index"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入名称"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="音乐" prop="url">
        <el-input
          v-model="form.url"
          readonly
          placeholder="请上传音乐,大小不能超过5MB"
          class="imginput"
        >
          <template slot="append">
            <div class="commhover full_w inputbox">
              上传音乐
              <input
                type="file"
                name=""
                class="uploadinput commhover"
                id="uploadimg"
                accept="audio/*"
                @change="changeImg"
              />
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item v-if="imgsrc">
        <audio controls :src="imgsrc"></audio>
        <!-- <img :src="imgsrc" alt="" class="imgurl" /> -->
      </el-form-item>
      <el-form-item label="排序" prop="level" v-if="!row">
        <el-input type="number"
          v-model="form.level"
          placeholder="排序值（0-10000）"
          maxlength="10"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="flexend">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="handleCloseAdd()">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { isOverSize } from "@/utils/validate";
import { uploadPhoto } from "@/api/comm";
import { musicAdd, musicEdit} from '@/api/material'
export default {
  data() {
    return {
      form: {},
      imgsrc:'',
      classList: [],
      // 表单校验
      rules: {
        class_id: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        url: [
          { required: true, message: "预览图不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        level: [
          { required: true, message: "排序值不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created(){
    this.reset()
    if(this.row){
      this.form.id = this.row.id
      this.form.name = this.row.name
      this.form.class_id = this.row.sc_id
      this.form.status = this.row.status
      this.form.url = this.row.url
      this.imgsrc = process.env.VUE_APP_BASE_API + "/" + this.row.url;
    }
    this.getDicts('/manage/index/resourceClass').then(res=>{
      this.classList = res.data
    })
  },
  props:{
    row:{
      default:null
    }
  },
  methods: {
    reset() {
      this.form = {
        id: undefined,
        class_id: undefined,
        name: undefined,
        url: undefined,
        level: undefined,
        status: 1
      };
      this.imgsrc = ''
      this.resetForm("form");
    },

    handleCloseAdd(type) {
      this.$emit("close", type);
    },

    // 上传图片
    changeImg(event) {
      var file = event.currentTarget.files[0];
      console.log("file:", file, event);
      if (isOverSize(file.size, 5)) {
        this.$message.warning("背景音乐大小不能超过5MB");
        return;
      }
      var formData = new FormData();
      formData.append("file", file);
      formData.append("type", 'music');
      uploadPhoto(formData).then((res) => {
        if (res.code == 1) {
          // console.log('图片：',res.data);
          this.$message.success("上传成功");
          // let url = res.data.replace("\\", "/");
          this.imgsrc = process.env.VUE_APP_BASE_API + "/" + res.data;
          this.form.url = res.data;
          document.getElementById("uploadimg").value = null;
        }
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            musicEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.reset()
              this.$emit('close',1)
            });
          } else {
            musicAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.reset()
              this.$emit('close',1)
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.inputbox {
  position: relative;
  padding: 0 20px;
  cursor: pointer;
}
.uploadinput {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  cursor: pointer;
}
.imginput {
  ::v-deep .el-input-group__append {
    padding: 0 !important;
  }
}
.imgurl {
  width: 200px;
  height: 80px;
}
</style>